/*
 * @(#)UUID.java 2009-4-5
 * 
 * Copyright 5jxiang . All rights reserved.
 */
package com.jz.util;

import java.util.Random;

/**
 * <p>
 * <a href="UUID.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author 5jxiang
 * @version $Id$
 */
public class UUID {
	public static String randomUUID() {
		java.util.UUID uuid = java.util.UUID.randomUUID();
		long mostSigBits = uuid.getMostSignificantBits();
		long leastSigBits = uuid.getLeastSignificantBits();
		return (digits(mostSigBits >> 32, 8) + 
				digits(mostSigBits >> 16, 4) + 
				digits(mostSigBits, 4) + 
				digits(leastSigBits >> 48, 4) + 
				digits(leastSigBits, 12));
	}
	/** Returns val represented by the specified number of hex digits. */
	private static String digits(long val, int digits) {
		long hi = 1L << (digits * 4);
		return Long.toHexString(hi | (val & (hi - 1))).substring(1);
	}
	
	private final static char [] NUMBER_ARRAY = {'1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','V','U','W','X','Y','Z'};
	
	public static String randomNumber(int length){
		int i = 0;
		String result = "";
		Random random = new Random();
		do{
			int rand = random.nextInt(NUMBER_ARRAY.length);
			result += NUMBER_ARRAY[rand];
			i++;
		}while(i < length);
		
		return result;
	}
	
	public static int[] randomArray(int length) {
		if (length <= 0)return null;
		int [] result = new int[length];
		int i = 0;
		int temp, value;
		Random random = new Random();
		for (i = 0; i < length; i++) {
			result[i] = i;
		}
		for (i = 0; i < length - 1; i++) {
			value = random.nextInt(length - i);
			temp = result[value];
			result[value] = result[length - i - 1];
			result[length - i - 1] = temp;
		}
		return result;
		
	}
	
	public static void main(String args[]) {
		for (int i = 2; i < 10; i++) {
			int [] x = randomArray(i);
			for (int j = 0; j < x.length; j++) {
				System.out.print(x[j] + "  ");
			}
			System.out.println("  ");
		}
	}
}
